using System;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using rlTodayMakeSkin.Project.Attributes;


namespace rlTodayMakeSkin.Project.XmlElement
{
    /// <summary>
    /// Represents a Registry xml element of the skin
    /// </summary>
    public class rlRegistry : ContainerSubElements
    {

        /*****************************
         FIELDS
         **************************/
        private Color colore;
        private string regKey;
        private string align;
        private string altText;

        /*****************************
         CONSTRUCTORS
         **************************/

        public rlRegistry(XmlNode n, rlTodayProject prj)
            : base(n, prj)
        {
            initElement();
            Key = n.Attributes["key"].Value;
        }

        public rlRegistry(rlTodayProject prj)
            : base(prj)
        {
            initElement();
        }

        public rlRegistry() { } //for the serialization
    

        /*****************************
         PUBLIC MEMBERS
         **************************/

        /// <summary>
        /// Color of the text of the registry key value
        /// </summary>
        [XmlIgnore()]
        [PropertyDescription("Sets, gets color of the text of the registry key value", PropertyTypes.COLOR)]
        public Color TextColor{
            get { return colore; }
            set { colore = value;}
        }

        /// <summary>
        /// Value of the TextColor property in RGB(n,n,n) format
        /// </summary>
        [XmlAttribute("color")]
        [PropertyDescription("Sets, gets value of the TextColor property in RGB(n,n,n) format", ReadOnlyProperty = true)]
        public string RGBColor
        {
            get { return GetRGBFromColor(colore); }
            set { /*do nothing: impelemented only for serialization*/}
        }

        /// <summary>
        /// Compelete name of the registry key
        /// </summary>
        [PropertyDescription("Sets, gets compelete name of the registry key", PropertyTypes.REGISTRY_VALUE)]
        [XmlIgnore()]
        public string Key
        {
            get { return regKey; }
            set { regKey = value;}
        }

        /// <summary>
        /// Returns the value of the actual key. If the current value doesn't contain
        /// \\ then read the actual value from RegistryKey.xml file
        /// </summary>
        [XmlAttribute("key")]
        public string KeyValue
        {
            get {
                if (regKey.StartsWith("HKEY"))
                    return regKey;
                else
                    return rlTodayMakeSkin.GUI.ProjectsManager.GetRegKeyValue(regKey);
            }
            set {
                regKey = rlTodayMakeSkin.GUI.ProjectsManager.GetNameKeyFromValue(value);
                if (regKey == "") regKey = value;
            }
        }

        /// <summary>
        /// Alignment of the text
        /// </summary>
        [PropertyDescription("Sets, gets alignment for the text", PropertyTypes.ALIGNMENT)]
        [XmlAttribute("alignment")]
        public string alignment
        {
            get { return align; }
            set { align = value; }
        }

        /// <summary>
        /// Alternate text showed if registry key is not found
        /// </summary>
        [PropertyDescription("Sets, alternate text showed if registry key is not found")]
        [XmlAttribute("unknown")]
        public string unknown
        {
            get { return altText; }
            set { altText = value; }
        }

        public override void DrawElement(Graphics g)
        {
            //if Key property is blank showd [REG_KEY] text, otherwise show
            //the name of the registry key
            string txt = (regKey.Equals("") ? "[REG KEY]" : regKey);
            if (txt.StartsWith("HKEY")) txt = txt.Substring(txt.LastIndexOf("\\") + 1);
            DrawString(g, txt, font, colore);
        }



        private void initElement(){
            hourHand = null;
            strNome = "Registry";
            altText = "";
            regKey = "";
            colore = Color.Black;
        }
        
    }//end class

}
